<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">
      <el-form-item label="券码" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入券码"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="券类型" prop="couponType">
        <el-select style="width: 240px" v-model="queryParams.couponType" placeholder="请选择券类型" clearable size="small">
          <el-option
            v-for="dict in couponTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户电话" prop="custMobile">
        <el-input
          v-model="queryParams.custMobile"
          placeholder="客户电话"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="custRealname">
        <el-input
          v-model="queryParams.custRealname"
          placeholder="请输入客户名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发券经纪人" prop="fqAgentRealname">
        <el-input
          v-model="queryParams.fqAgentRealname"
          placeholder="请输入发券经纪人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发券经纪人电话" prop="fqAgentMobile">
        <el-input
          v-model="queryParams.fqAgentMobile"
          placeholder="请输入发券经纪人电话"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发券时间">
        <el-date-picker
          v-model="fqCreateTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="发券大区" prop="fqDq">
        <el-input
          v-model="queryParams.fqDq"
          placeholder="请输入发券大区"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发券事业部" prop="fqSyb">
        <el-input
          v-model="queryParams.fqSyb"
          placeholder="请输入发券事业部"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核销经纪人" prop="hxAgentRealname">
        <el-input
          v-model="queryParams.hxAgentRealname"
          placeholder="请输入核销经纪人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核销经纪人电话" prop="hxAgentMobile">
        <el-input
          v-model="queryParams.hxAgentMobile"
          placeholder="请输入核销经纪人电话"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核销时间">
        <el-date-picker
          v-model="hxTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="核销大区" prop="hxDq">
        <el-input
          v-model="queryParams.hxDq"
          placeholder="请输入核销大区"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核销事业部" prop="hxSyb">
        <el-input
          v-model="queryParams.hxSyb"
          placeholder="请输入核销事业部"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 导出 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['marketingStat:coupons:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border :data="getCouponDetails" @sort-change="sortData">
      <el-table-column label="券码" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.serialNumber | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="券类型" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.couponType | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户电话" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.custMobile | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.custRealname | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发券经纪人" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.fqAgentRealname | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发券经纪人电话" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.fqAgentMobile | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="fqCreateTime" label="发券时间" width="180" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.fqCreateTime ? parserDate(scope.row.fqCreateTime) : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发券店组" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.fqoShopName | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发券大区" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.fqDq | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发券事业部" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.fqSyb | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核销经纪人" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.hxAgentRealname | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核销经纪人电话" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.hxAgentMobile | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="hxTime" label="核销时间" width="180" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.hxTime ? parserDate(scope.row.hxTime) : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核销店组" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.hxoShopName | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核销大区" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.hxDq | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核销事业部" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.hxSyb | showTxt }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listCoupon, exportPost } from "@/api/marketingStat/couponsDetail";

export default {
  name: "Coupon",
  data() {
    return {
      loading: false,
      // 总条数
      total: 0,
      // 发券时间范围
      fqCreateTime: [],
      // 核销时间范围
      hxTime: [],
      // 券类型数据字典
      couponTypeOptions: [],
      // 券明细列表
      getCouponDetails: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNumber: undefined, // 券码
        couponType: undefined, // 券类型
        custMobile: undefined, // 客户电话
        custRealname: undefined, // 客户名称
        fqAgentRealname: undefined, // 发券经纪人
        fqAgentMobile: undefined, // 发券经纪人电话
        fqDq: undefined, // 发券大区
        fqSyb: undefined, // 发券事业部
        hxAgentRealname: undefined, // 核销经纪人
        hxAgentMobile: undefined, // 核销经纪人电话
        hxDq: undefined, // 核销大区
        hxSyb: undefined, // 核销事业部
        orderBy: '7 asc'
      }
    };
  },
  filters: {
    showTxt(txt) {
      return txt ? txt : '--' 
    }
  },
  created() {
    this.getCouponTypeDisc()
    this.getList();
  },
  methods: {
    /** 券类型字典 **/
    getCouponTypeDisc() {
      this.getDicts("mkt_coupons_type").then(response => {
        this.couponTypeOptions = response.data;
      });
    },
    /** 查询领券明细列表 */
    getList() {
      this.loading = true;
      if (this.fqCreateTime.length) {
        this.addCouponDateRange('fqCreateTime', this.queryParams, this.fqCreateTime)
      }
      
      if(this.hxTime.length) {
        this.addCouponDateRange('hxTime', this.queryParams, this.hxTime)
      }
      
      listCoupon(this.queryParams).then(
        response => {
          this.getCouponDetails = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.fqCreateTime = [];
      if (this.queryParams.fqCreateTimeBegin) {
        this.addCouponDateRange('fqCreateTime', this.queryParams, this.fqCreateTime)
      }
      this.hxTime = [];
      if (this.queryParams.hxTimeBegin) {
        this.addCouponDateRange('hxTime', this.queryParams, this.hxTime)
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //** 排序 */
    sortData(obj) {
      let index = obj.column.id.split('_')[3]
      if (obj.order) {
        this.queryParams.orderBy=`${index} ${/asc/.test(obj.order) ? 'asc' : 'desc'}`
      }
      this.getList()
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有领券明细?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportPost(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function() {});
    }
  }
};
</script>
